package com.woniu.controller.contemtService;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.model.contentMService.entity.Permission;
import com.woniu.model.contentMService.vo.PageVo;
import com.woniu.model.contentMService.vo.PermissonMenuVo;
import com.woniu.model.gh.common.Result;
import com.woniu.service.contentService.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限等级分配表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-10-26
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/findPermission")
    public Result findPermission(Long managerId) {
        List<PermissonMenuVo> list = permissionService.findPermissionByManagerId(managerId);
        return Result.ok(list);
    }

    @GetMapping("/findAll")
    public Result findAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, String permissionName) {
        PageVo pageVo = permissionService.findAll(pageNum,pageSize,permissionName);
        return Result.ok(pageVo);
    }

    @GetMapping("/findFirstLevelMenu")
    public Result findFirstLevelMenu() {
        //查询所有的parent——id是-1的菜单，父菜单
        List<Permission> list = permissionService.list(Wrappers.lambdaQuery(Permission.class).eq(Permission::getParentId, -1));
        return Result.ok(list);
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Permission permission) {
        if (permission.getId() == null) {
            //添加操作: 如果添加权限时候没有选择“父级菜单”，说明是一级菜单，需要设置parentId是-1
            if (permission.getParentId() == null) {
                permission.setParentId(-1L);
            }
            permissionService.save(permission);
        }else {
            // 修改操作
            permissionService.updateById(permission);
        }
        return Result.ok();
    }

    //权限树
    @GetMapping("/list")
    public Result list() {
        List<PermissonMenuVo> list = permissionService.treeList();
        return Result.ok(list);
    }


}
